package com.foriseholdings.algorithm.taskscheduling.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.foriseholdings.algorithm.Itemcf.ItemCFJobRunner;
import com.foriseholdings.algorithm.taskscheduling.model.ResultModer;
import com.foriseholdings.algorithm.topN.TopNJobRunner;
import com.foriseholdings.algorithm.usercf.UserCFJobRunner;
import com.foriseholdings.commons.common.BaseRunner;
import com.foriseholdings.commons.utils.PropertyUtil;
import com.foriseholdings.parseLogs.ParseMain2;
import com.foriseholdings.upload2hdfs.UploadToHdfs;

public class TimerTask {

	// 商品ID 跟用户id 会不会出现通明情况
	static Logger logger = Logger.getLogger(TimerTask.class);

	
	public static final String GET_URL = PropertyUtil.getProperty("GET_URL");
	public static final String UPDATE_URL = PropertyUtil.getProperty("UPDATE_URL");
	
	
	// public static final String GET_URL =
	// "http://223.223.202.230:8880/task/getBookCodes/dap";
//	public static final String GET_URL = "http://192.168.92.7:8880/task/getBookCodes/dap";
	// public static final String GET_URL =
	// "http://localhost:8880/task/getBookCodes/dap";

	// public static final String UPDATE_URL =
	// "http://223.223.202.230:8880/task/update/1";
//	public static final String UPDATE_URL = "http://192.168.92.7:8880/task/update/1";
	public static final String FAILL = "111111";
	public static final String NOTASK = "222222";
	public static String task_url = null;
	BaseRunner baseRun;

	public static void main(String[] args) {
		TimerTask tt = new TimerTask();
		boolean flag = false;
		String responseJson = RequestTask.sendPost("", GET_URL);
		// // 需要替换掉@ 符号如果不替换 解析json报错
		String orgJson = responseJson.replace("@", "//");
		List<String> busCodeInfo = new ArrayList<String>();
		busCodeInfo = ParseJson.getbusCodeInfo(orgJson);
		for (String busCode : busCodeInfo) {

			 task_url = "http://223.223.202.230:8880/task/get/" + busCode + "/10";
//			task_url = "http://192.168.92.7:8880/task/get/" + busCode + "/10";
			responseJson = RequestTask.sendPost("", task_url);
			orgJson = responseJson.replace("@", "//");
			ResultModer resultModer = ParseJson.getBaseInfo(orgJson);

			if (resultModer == null) {

				logger.info(new Date());
				logger.info("未获取到任务");
				logger.info(new Date());
				continue;
			}

			// 获取任务失败，调用通知接口返回
			if (resultModer.getCode().equals(FAILL) || resultModer.getCode().equals(NOTASK)) {
				logger.info("code is 111111 或者 222222");
				flag = false;
			} else {
				String log_path = resultModer.getFtpPath();
				// 上传
				String buss_code = resultModer.getBusCode();
				UploadToHdfs.run(log_path, buss_code);
				// /String buss_code = "BC1001";
				 flag = tt.getTask(buss_code);
				if (flag) {
					JSONObject jsonObj = JSONObject.parseObject(orgJson);
					jsonObj.clear();
					// JSONObject jsonObj = new JSONObject();
					// jsonObj.put("buscode", buss_code);
					jsonObj.put("id", resultModer.getId());
					jsonObj.put("flag", "2");
					jsonObj.put("reason", "调取任务执行成功");
					// System.out.println(jsonObj.toString());
					RequestTask.sendPost(jsonObj.toString(), UPDATE_URL);
					System.out.println("更新成功");
					// System.exit(0);
				}
			}
		}
		System.exit(0);
	}

	public boolean getTask(String buss_code) {
		// String[] busiCodes = { "XC1006" };
		// for (String buss_code : busiCodes) {
		System.out.println("开始hadoop");
		boolean flag = false;
		// 解析日志 解析输出结果 是所有算法的输入， 解析输入 是上传路径
		baseRun = new ParseMain2();
		flag = baseRun.baseStart(buss_code);
		// flag = ClearDataTable.clearTb(buss_code);
		// topN算法
		if (flag) {
			baseRun = new TopNJobRunner();
			flag = baseRun.baseStart(buss_code);
		}
		// 基于物品
		if (flag) {
			baseRun = new ItemCFJobRunner();
			flag = baseRun.baseStart(buss_code);
		}
		if (flag) {
			baseRun = new UserCFJobRunner();
			flag = baseRun.baseStart(buss_code);
		}
		return flag;
	}

	// public static void main(String[] args) {
	// Runnable runnable = new Runnable() {
	// public void run() {
	// // task to run goes here
	// String jsonString = RequestTask.httpURLConectionGET(GET_URL);
	// // System.out.println(jsonString);
	// if (!jsonString.equals("fail")) {
	// if (ParseJson.getBaseInfo(jsonString)) {
	//
	// UploadToHdfs.run("/tmp/recommendation/");
	// //// 解析文件
	//
	// // baseRun = new ContentJobRunner();
	// // baseRun.begin();
	//
	// 新增加业务系统
	// String parm = " {\"id\":\"14001\",\"job_state\":\"2\",\"reason\":\"成功\"}";
	// try {
	// RequestTask.postBody(UPDATE_URL, parm);
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// System.exit(0);
	// }
	// } else {
	// System.out.println("沒有任務");
	// }
	// }
	// }
	// };
	// ScheduledExecutorService service =
	// Executors.newSingleThreadScheduledExecutor();
	// // 第二个参数为首次执行的延时时间，第三个参数为定时执行的间隔时间 单位是秒 7200
	// // 每兩小時執行
	// service.scheduleAtFixedRate(runnable, 10, 7200, TimeUnit.SECONDS);
	// }
}
